package org.example;

/**
 * @Title : aa
 * @Description: :
 * @author: libo@juzishuke.com
 * @date: 2023/11/29 11:31
 * @Version:1.0
 */

import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Group;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
@Slf4j
public class JMHModTest {

    Long size = 0L+Integer.MAX_VALUE*10;
    long mod  = 0xffffffffL;
    long data = 0L;
    @Setup(Level.Iteration)
    public void seTup(){
        data = Math.round(Math.random()*Long.MAX_VALUE);
//        log.info("{}",data);
    }

    @Benchmark
    @Group
    public void modTest(Blackhole blackhole) {
        blackhole.consume(data % mod);
    }

    @Benchmark
    @Group
    public void bitModTest(Blackhole blackhole) {
        blackhole.consume(data & 0xffffffffL);
    }

    public static void main(String[] args) throws RunnerException {
        final Options opts = new OptionsBuilder().include(JMHModTest.class.getSimpleName())
                .forks(1).measurementIterations(10000).warmupIterations(3).build();
        new Runner(opts).run();
    }
}

